
%macro prnt2(program=, disname=, ds_list=, vis=, popfl=,tblno=);

%get_data(lst=adqs adsl,pre=,lib=adamw,supp=y);

data qs1;
	set adqs;
	where pdfl='Y' and %if &tblno=2 %then %do; upcase(parcat1)='TOBACCO/NICOTINE WITHDRAWAL' %end; 
	%else %if &tblno=3 %then %do; upcase(parcat1)='DIRECT EFFECTS OF PRODUCT' %end;;
	if trta='A' then do;
		trtpn=trtan;
		trtp=trta;
		column=1;
	end;
	if trta='B' then do;
		trtpn=trtan;
		trtp=trta;
		column=2;
	end;
	if trta='C' then do;
		trtpn=trtan;
		trtp=trta;
		column=3;
	end;
	if trta ne '' then output;
	trtpn=4;
	trtp='Overall';
	column=4;
	output;
run;

data pop;
	set adsl;
	where pdfl='Y';
	array trts trt01pn trt02pn trt03pn trt04pn trt05pn trt06pn;
	do over trts;
		if trts=1 then do;
			column=1;
			trtpn=1;
			trtp='Product A';
			output;
		end;
		if trts=2 then do;
			column=2;
			trtpn=2;
			trtp='Product B';
			output;
		end;
		if trts=3 then do;
			column=3;
			trtpn=3;
			trtp='Product C';
			output;
		end;
	end;
	column=4;
	trtpn=4;
	trtp='Overall';
	output;
run;

data pop;
	set pop;
	tocount=1;
run;

Proc sort data =pop out = spopone nodupkey ;
   by column trtp;
run;
Proc Print data = spopone noobs n width = min;
var column trtp;
run;
data tofmt;
   set spopone;
run;

%makefmt(tofmt, column , trtp, _ftrt);
*******************************************************************************************************
Define population for each column
******************************************************************************************************;
%popu(datain=pop);
%macro a;
%do i = 1 %to &maxtrt;
   %put pop&i=&&&pop&i;
%end;
%mend a; %a;

data _heads;
  format n1-n4 $15. lbl1-lbl4 $150.;

  n1 = "(N=%cmpres(&pop1))";
  n2 = "(N=%cmpres(&pop2))";
  n3 = "(N=%cmpres(&pop3))";
  n4 = "(N=%cmpres(&pop4))";
  *n5 = "(N=%cmpres(&pop5))";

  lbl1 = strip(put(1, _ftrt.));
  lbl2 = strip(put(2, _ftrt.));
  lbl3 = strip(put(3, _ftrt.));
  lbl4 = strip(put(4, _ftrt.));
  *lbl5 = strip(put(5, _ftrt.));

  call symput("N1",n1);
  call symput("N2",n2);
  call symput("N3",n3);
  call symput("N4",n4);
  *call symput("N5",n5);

  call symput("lbl1",lbl1);
  call symput("lbl2",lbl2);
  call symput("lbl3",lbl3);
  call symput("lbl4",lbl4);
  *call symput("lbl5",lbl5);
run;

proc sql noprint;
create table headers
(column num,
  header char(200));
insert into headers
   %do i = 1 %to 4;
      values(&i, "&&lbl&i.~%str(&&N&i)")
   %end;
;

select header
into :header1-:header99
from headers
where column LE 4
order by column;
quit;

*******************************************************************************************************
Create columns macros
******************************************************************************************************;
data _null_;
   call symput("nbr_report_column", "4");
   call symput("column1", "COL1");
   call symput("column2", "COL2");
   call symput("column3", "COL3");
   call symput("column4", "COL4");
   
run;



*******************************************************************************************************
Define specific formats for the report
******************************************************************************************************;
proc format;
 value level1l  1 = "Value"
                2 = "Change From Baseline"
;
run;

*******************************************************************************************************
Data manipulation
******************************************************************************************************;
proc sort data=qs1;
	by param avisitn avisit parcat2 atptn atpt column;
run;

%univariate6c(var=aval, width=5,decimal=0,out=_tmp1,level1=1,level1FMT=level1l,level2=,level2FMT=stat4_,level3=,wh=%str(),
			by= param avisitn avisit parcat2 atptn atpt, in=qs1);

%univariate6c(var=chg, width=5,decimal=0,out=_tmp2,level1=2,level1FMT=level1l,level2=,level2FMT=stat4_,level3=,wh=%str(chg ne . and ablfl ne "Y"),
			by= param avisitn avisit parcat2 atptn atpt, in=qs1);

proc sort data=qs1;
	where avisitn in (4 5 6);
	by param  parcat2 atptn atpt column;
run;
%univariate6c(var=aval, width=5,decimal=0,out=_tmp3,level1=1,level1FMT=level1l,level2=,level2FMT=stat4_,level3=,wh=%str(),
			by= param  parcat2 atptn atpt , in=qs1);
proc sort data=qs1;
	where avisitn in (4 5 6);
	by param  parcat2 atptn atpt column;
run;

%univariate6c(var=chg, width=5,decimal=0,out=_tmp4,level1=2,level1FMT=level1l,level2=,level2FMT=stat4_,level3=,wh=%str(chg ne . and ablfl ne "Y"),
			by= param  parcat2 atptn atpt, in=qs1);


data  final1;
	set _tmp1 _tmp2 _tmp3(in=a) _tmp4(in=b);
	if a then do;
	avisitn=7;
	avisit="Overall";
	end;
	if b then do;
	avisitn=7;
	avisit="Overall";
	end;
run;
data final;
	set final1;
	if LEVEL2_LABEL="N" then LEVEL2_LABEL="n";
run;
 


*******************************************************************************************************
Create report dataset
******************************************************************************************************;
/*data final;*/
/*	set _tmp1 _tmp2 ; */
/*	by  param avisitn avisit parcat2 atptn atpt level1 level2;*/
/*	if LEVEL2_LABEL="N" then LEVEL2_LABEL="n";*/
	/*txt = put(level1,level1l.);*/
	/*where level2 ne 4;*/
/*	drop level1_label;*/
/*run;*/

data final;
	set final;
	length level1c $100.;
	level1c = put(level1,level1l.);
	if level2>2 then level2=level2+1;
	if level2=7 then level2=3;
run;


proc sort data=final out=report;
	by param avisitn avisit parcat2 atptn atpt level1  level2  ;
run;

proc sql;
 select count(distinct level2_label)
 into :nb_obse separated by ' '
 from report;
quit;

%page_cut(in=report,sort=param avisitn avisit parcat2 atptn atpt level1 level1c level2 level2_label col1 col2 col3 col4,
nocut=atpt,pagesize=32,skip=level1c,
/*order=level1 avisitn avisit parcat2 atptn atpt level2,*/
columns=param avisit parcat2 atpt level1c level2_label col1 col2 col3 col4,
width=  1 0.8 1 1 1 1 1 1 1 1,out=repport);

data repport;
	set repport;
	if index(param,"Maximum Observed Effect")  then delete;
run;


*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&disname..QS.DESC.STAT.rtf";

%open_rtf;
%empty_ds_fix(report);

%if &tblno=2 %then %do;
title4 "Tobacco/Nicotine Withdrawal – Descriptive Statistics" ;
%end;

%if &tblno=3 %then %do;
title4 "Direct Effect of Product Questionnaire – Descriptive Statistics" ;
%end;

title5 "(Pharmacodynamic Population)";

/*footnote3 j=l "Note(s): N is by parameter either per subjects or per units.";*/
/*footnote4 j=l "         Estimated nicotine uptake = total cartridge weight change (mg) x nicotine concentration of the liquid (% by weight)";*/
/*footnote5 j=l " ";*/
/*footnote6 j=l " ";*/
/*footnote7 j=l " ";*/


proc report data=repport nowd split='~' missing;
 column page param avisitn avisit parcat2 atptn atpt level1 level1c level2 level2_label col1 col2 col3 col4;
 define page         / " " order order=internal noprint;
 define level1       / " " order order=internal noprint;
 define level2       / " " order order=internal noprint;
 define avisitn       / "" order order=internal noprint;
 define atptn       / "" order order=internal noprint;
 define Param / "Item" order group order=internal style(column)=[cellwidth=&__param.in just=left] style(header)=[just=left];
 define avisit / "Visit" order group order=internal style(column)=[cellwidth=&__avisit.in just=left] style(header)=[just=left];
 define parcat2 / "Product Use~Session" order group order=internal style(column)=[cellwidth=&__parcat2.in just=left] style(header)=[just=left];
 define atpt / "Time Point" order group order=internal style(column)=[cellwidth=&__atpt.in just=left] style(header)=[just=left];
 define level1c / " " order group order=internal style(column)=[cellwidth=&__level1c.in just=left] style(header)=[just=left];
 
 define level2_label / " " order order=internal style(column)=[cellwidth=&__level2_label.in just=left] style(header)=[just=left];

 %do j = 1 %to 4; 
 	define col&j / "&&header&j." style(column)=[cellwidth=&&__col&j..in just=center] style(header)=[just=center];
 %end;

 
 break after page   / page;
 %if &nb_obse GT 0 %then %do;

 compute before level1c / style=[cellwidth=6in just=left];
  line " ";
 endcomp;
 %end;

 %if &nb_obse EQ 0 %then %do;
   compute after page/style=[cellwidth=9.7in just=left];
    line "No Values Were Measured.";
   endcomp;
 %end;

run;

ods rtf close;
ods listing;
%arrange_rtf;
%mend prnt2;

/*%prnt(program=T14_01_04_02_PROD_USE, disname=14.1.4.2, ds_list=ADEX, vis=%str((4 5 6), popfl=%str(SAFFL="Y"));*/
%prnt2(program=T14_02_03_01_0X_QS, disname=14.2.3.1.2, ds_list=ADQS ADSL,tblno=2);
%clrw;

%prnt2(program=T14_02_03_01_0X_QS, disname=14.2.3.1.3, ds_list=ADQS ADSL,tblno=3);
%clrw;
